package com.cliang.tools.encription;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 加密工具类
 */
public class Encription {

    /**
     * 使用MD5进行加密
     * @param str   加密前字符串
     * @return newStr 加密后的字符串
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static  String encryptionByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String newStr="";
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        Base64.Encoder base64Encoder=Base64.getEncoder();

        //加密后的字符串
        newStr=base64Encoder.encodeToString(md5.digest(str.getBytes("utf-8")));
        return newStr;
    }


    /**
     * MD5加密验证
     * @param str 待验证字符串
     * @param eqStr 加密后作为比较的字符串
     * @return true:验证成果;false:验证失败
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static boolean checkEncryptionByMD5(String str,String eqStr) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if(encryptionByMD5(str).equals(eqStr)){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 使用SHA-256加密
     * @param str 待加密字符串
     * @return newStr 加密后字符串
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static  String encryptionBySHA256(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String newStr="";
        MessageDigest sha256=MessageDigest.getInstance("SHA-256");
        Base64.Encoder base64Encoder=Base64.getEncoder();

        newStr=base64Encoder.encodeToString(sha256.digest(str.getBytes("utf-8")));
        return newStr;
    }

    /**
     * SHA256加密验证
     * @param str 待验证字符串
     * @param eqStr 加密后作为比较的字符串
     * @return true:验证成果;false:验证失败
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static boolean checkEncryptionBySHA256(String str,String eqStr) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if(encryptionBySHA256(str).equals(eqStr)){
            return true;
        }else{
            return false;
        }
    }

    public static String encodeByMD5(String str){
        StringBuffer md5=new StringBuffer();
        try {
            MessageDigest md=MessageDigest.getInstance("MD5");
            md.update(str.getBytes("utf-8"));
            byte[] digest=md.digest();

            for(int i=0;i<digest.length;i++){
                md5.append(Character.forDigit((digest[i] & 0xF0) >> 4,16));
                md5.append(Character.forDigit((digest[i] & 0xF),16));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return md5.toString();
    }
}
